package com.qsd.StudentSystem.controller;

import com.qsd.StudentSystem.service.StudentService;
import com.qsd.common.enums.ResultStatus;
import com.qsd.common.po.Student;
import com.qsd.common.vo.BaseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("student")
public class StudentController {

	@Resource
	private StudentService studentService;
	
	@GetMapping("login")
	public BaseVo login(String username, String password, HttpServletRequest request) {
		log.info("用户登录控制");
		if(username != null && password != null && !"".equals(username) && !"".equals(password)) {
			ResultStatus resultStatus = studentService.login(username, password, request);
			return new BaseVo(resultStatus);
		}
		return new BaseVo(ResultStatus.USERNAME_OR_PASSWORD_MISS);
	}
	
	@PostMapping("register")
	public BaseVo register(Student student, HttpServletRequest request) {
		log.info("学生注册:{}",student);
		return new BaseVo(studentService.register(student, request));
	}
	
	@GetMapping("exit")
	public BaseVo exit(HttpServletRequest request) {
		log.info("退出用户登录");
		request.getSession().removeAttribute("student");
		return new BaseVo(ResultStatus.SUCCESS);
	}

}
